/*
 * Copyright (c) 2020 hebeiyiliuba.com
 * All rights reserved.
 *
 */
package org.jeecg.modules.third.openapi;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.openapi.sdk.service.DataExchangeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.constant.OpenApiConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.third.jg.report.freight.information.constant.enums.PaymentMeansCodeEnum;
import org.jeecg.modules.third.openapi.dto.report.BizParamDTO;
import org.jeecg.modules.third.openapi.dto.report.ZjDriverDTO;
import org.jeecg.modules.third.openapi.dto.report.ZjVehicleDTO;
import org.jeecg.modules.third.openapi.dto.report.capital.CapitalDTO;
import org.jeecg.modules.third.openapi.dto.report.capital.ZjFinanciallist;
import org.jeecg.modules.third.openapi.dto.report.capital.ZjShippingNoteList;
import org.jeecg.modules.third.openapi.dto.report.waybill.*;
import org.jeecg.modules.third.openapi.enums.ErrorCodeEnum;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyVehicleService;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * 中交兴路OpenApi服务
 * 使用 SDK 接口客户端调用示例(如不使用 SDK，可通过标准 HTTPS 方式调用接口，忽略正式信任。)
 * 该文件使用 UTF-8 编码格式
 * @author duanlikao
 * @created 2020/12/14
 */
@Service
@Slf4j
public class OpenApiService {

    /**
     *  上报环境：1 测试，2 生产
     */
    //private static final int  REPORT_ENV = 1;

    /**
     * API请求地址
     */
    //"https://zhiyunopenapi.95155.com/save/apis/";
    @Value(value="${openapi.api_domain:}")
    private String API_DOMAIN ;

    @Value(value="${openapi.reportenv:1}")
    private Integer REPORT_ENV;

    @Value(value="${openapi.callbackurl:}")
    private String REPORT_CALLBACK_URL;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private IHyVehicleService hyVehicleService;


    private DataExchangeService getDataExchangeService() throws Exception {
        return new DataExchangeService(5000,5000);
    }

    /**
     * 用户登陆
     * 用户首次调用接口，须先登录，认证通过后生成令牌。
     * 令牌，多服务调用业务接口时，建议由统一服务调用登录接口将令牌缓存起来，多个服务统一从
     * 共享缓存中获取令牌。
     * 为安全考虑，建议用户自己每月至少更新一次令牌。
     * 令牌失效后再调用登录接口获取令牌，避免频繁调用登录接口，建议一天内登录次数不超过10次，
     * 超过 10 次将触发安全系统报警。
     */
    private String getLoginToken() {
        HyConfig systemConfig = HyConfigUtils.getConfig();
        //systemConfig.setZjxlIzUse(true);
        //systemConfig.setZjxlApiAccount("c03ea706-99dd-495e-b11c-cba84cf501a8");
        //systemConfig.setZjxlApiPassword("F2aL6d64XAd593J35644X951U0xY87");
        //systemConfig.setZjxlApiClientid("872ea103-7f66-4faf-9d31-cb10191ae4f0");
        //systemConfig.setZjxlApiPrivateKey("6d73c68a-5080-4f6c-9849-e6f4cfc72ddd");

        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理员");
        }
        Object tokenObject = redisUtil.get(OpenApiConstant.API_TOKEN_KEY);
        String token = "";
        if (tokenObject == null &&  StringUtils.isEmpty(token)) {
            try {
                Map <String, String> map = new HashMap <>();
                map.put("user",systemConfig.getZjxlApiAccount() );
                map.put("pwd", systemConfig.getZjxlApiPassword());
                map.put("srt", systemConfig.getZjxlApiPrivateKey());
                map.put("cid", systemConfig.getZjxlApiClientid());
                String url = API_DOMAIN+"login";
                // 通过 https 方式调用，此方法内部会使用私钥生成签名参数 sign,私钥不会发送
                log.info("获取中交服务token请求-> {}", JSONUtil.toJsonStr(map));
                String res = getDataExchangeService().postHttps(url, map);
                if(StringUtils.isBlank(res)){
                    log.info("OpenApiService getLoginToken  result|{}" ,res);
                    throw new JeecgBootException("数据获取异常,请稍后重试");
                }
                log.info("获取中交服务token回执<- {}", res);

                JSONObject jsonObject = JSONObject.parseObject(res);
                int status = jsonObject.getIntValue("status") ;
                token =  jsonObject.getString("result");
                if(ErrorCodeEnum.isValid(status)){
                    if(1001 == status && StringUtils.isNotEmpty(token)){
                        redisUtil.set(OpenApiConstant.API_TOKEN_KEY, token, OpenApiConstant.API_TOKEN_KEY_EXPIRE);
                        return token;
                    }
                    ErrorCodeEnum errorCodeEnum =  ErrorCodeEnum.getErrorCodeEnumByVal(status);
                    throw new JeecgBootException("数据获取异常:"+errorCodeEnum.getMsg());
                } else {
                    throw new JeecgBootException("数据获取异常:请稍后重试");
                }

            } catch (Exception e) {
                log.info("OpenApiService getLoginToken Exception |{}" ,e.getMessage());
                throw new JeecgBootException(e.getMessage());

            }
        }

        return tokenObject != null && StringUtils.isNotEmpty(tokenObject.toString())
                ? tokenObject.toString() : token;
    }


    /**
     * 运输时效管理
     * @param vclN
     * @param vco
     * @return
     */
    public String transTimeManage(String vclN ,String vco) {
        HyConfig systemConfig =  HyConfigUtils.getConfig();

        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
        }
        try {
            Map<String, String> map = new HashMap< >();
            String token = this.getLoginToken();

            if(StringUtils.isEmpty(token)){
                throw new JeecgBootException("中交兴路获取token失败,请稍后重试。");
            }
            map.put("token", token);
            map.put("srt", systemConfig.getZjxlApiPrivateKey());
            map.put("cid", systemConfig.getZjxlApiClientid());

            map.put("vnos", vclN+"_"+vco);
            //map.put("vclN", vclN);
            //map.put("vco", vco);
            //transTimeManage
            String url = API_DOMAIN+"transTimeManageV2";

            DataExchangeService des = getDataExchangeService();
            String res = des.postHttps(url, map);
            if(StringUtils.isBlank(res)){
                log.info("OpenApiService transTimeManage result|{}" ,res);
                throw new JeecgBootException("车辆实时轨迹数据获取异常,请稍后重试");
            }

            JSONObject jsonObject = JSONObject.parseObject(res);
            int status = jsonObject.getIntValue("status") ;
            if(ErrorCodeEnum.isValid(status)){
                if(1001 == status){
                    return    res ;
                }
                if(1016 == status || 1031 == status){
                    redisUtil.del(OpenApiConstant.API_TOKEN_KEY);
                    throw new JeecgBootException("中交兴路token失效,请稍后重试");
                }
                ErrorCodeEnum errorCodeEnum =  ErrorCodeEnum.getErrorCodeEnumByVal(status);
                throw new JeecgBootException("车辆实时轨迹数据获取异常:"+errorCodeEnum.getMsg());
            } else {
                throw new JeecgBootException("车辆实时轨迹数据获取异常:请稍后重试");
            }
        } catch (Exception e) {
            log.info("OpenApiService transTimeManage Exception |{}" ,e.getMessage());
            throw new JeecgBootException(e.getMessage());
        }
    }


    /**
     * 运输节点服务 V3
     * @param vnos
     * 车牌号_车牌颜色， 多个车辆以英文逗号分隔（最多 100 台车， 颜
     * 色： 1 蓝色、 2 黄色、 3黄绿） eg: 陕 YH0009_1,陕YH0008_2,陕 YH0007_3
     * @return
     */
    public String transTimeManageV3(String vnos) {

        HyConfig systemConfig =  HyConfigUtils.getConfig();
        if (systemConfig == null || !systemConfig.getZjxlIzUse()) {
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
        }

        //参数格式不正确
        if (!vnos.contains("_")) {
            return StrUtil.EMPTY;
        }

        try {
            Map<String, String> map = new HashMap< >();
            String token = this.getLoginToken();

            if (StrUtil.isEmpty(token)) {
                throw new JeecgBootException("中交兴路获取token失败,请稍后重试。");
            }
            map.put("token", token);
            map.put("srt", systemConfig.getZjxlApiPrivateKey());
            map.put("cid", systemConfig.getZjxlApiClientid());
            map.put("vnos", vnos);

            String url = API_DOMAIN + "transTimeManageV3";

            DataExchangeService des = getDataExchangeService();
            String res = des.postHttps(url, map);
            if (StrUtil.isBlank(res)) {
                log.info("OpenApiService transTimeManageV3 result|{}" ,res);
                throw new JeecgBootException("车辆运输节点数据获取异常,请稍后重试");
            }

            JSONObject jsonObject = JSONObject.parseObject(res);
            int status = jsonObject.getIntValue("status") ;
            if(ErrorCodeEnum.isValid(status)){
                if (1001 == status) {
                    return res ;
                }
                if (1016 == status || 1031 == status) {
                    redisUtil.del(OpenApiConstant.API_TOKEN_KEY);
                    throw new JeecgBootException("中交兴路token失效,请稍后重试");
                }

                ErrorCodeEnum errorCodeEnum =  ErrorCodeEnum.getErrorCodeEnumByVal(status);
                throw new JeecgBootException("车辆运输节点数据获取异常," + errorCodeEnum.getMsg());
            } else {
                throw new JeecgBootException("车辆运输节点数据获取异常,请稍后重试");
            }
        } catch (Exception e) {
            log.info("OpenApiService transTimeManage Exception |{}" ,e.getMessage());
            throw new JeecgBootException(e.getMessage());
        }
    }


    /**
     * 新版删除 2021/8/4
     * 车辆最新位置查询(车牌号)
     * 本接口提供指定车牌号的车辆最新位置查询。
     * @param vclN 车牌号
     * @return
     */
    public String  vLastLocationV3(String vclN) {
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
        }
        try {
            Map<String, String> map = new HashMap< >();
            String token = this.getLoginToken();

            if(StringUtils.isEmpty(token)){
                throw new JeecgBootException("获取token失败,请稍后重试。");
            }
            map.put("token", token);
            map.put("srt", systemConfig.getZjxlApiPrivateKey());
            map.put("cid", systemConfig.getZjxlApiClientid());
            map.put("vclN", vclN);
            map.put("timeNearby", "30");
            String url = API_DOMAIN+"vLastLocationV3";

            DataExchangeService des = getDataExchangeService();
            String res = des.postHttps(url, map);
            if(StringUtils.isBlank(res)){
                throw new JeecgBootException("数据获取异常,请稍后重试");
            }
            JSONObject jsonObject = JSONObject.parseObject(res);
            if(1016 == jsonObject.getIntValue("status")){
                redisUtil.del(OpenApiConstant.API_TOKEN_KEY);
                throw new JeecgBootException("token失效,请稍后重试");
            }

            return 1001 == jsonObject.getIntValue("status")  ?  res : "";
        } catch (Exception e) {
            log.info("OpenApiService vLastLocationV3 Exception |{}" ,e.getMessage());
            throw new JeecgBootException(e.getMessage());
        }

    }

    /**
     * 新版删除 2021/8/4
     * 车辆最新位置查询(车牌号+颜色)
     * @param vclN
     * @param vco 颜色(1 代表蓝色、2 代表黄色)
     * @return
     */
    public String opGpVclPosColor(String vclN ,Integer vco) {
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
        }
        try {
            Map<String, String> map = new HashMap<>();
            String token = this.getLoginToken();

            if(StringUtils.isEmpty(token)){
                throw new JeecgBootException("获取token失败,请稍后重试。");
            }
            map.put("token", token);
            map.put("srt", systemConfig.getZjxlApiPrivateKey());
            map.put("cid", systemConfig.getZjxlApiClientid());
            map.put("vclN", vclN);
            map.put("vco", vco.toString());
            map.put("timeNearby", "30");
            String url = API_DOMAIN+"opGpVclPosColor";

            DataExchangeService des = getDataExchangeService();
            String res = des.postHttps(url, map);
            if(StringUtils.isBlank(res)){
                throw new JeecgBootException("数据获取异常,请稍后重试");
            }
            JSONObject jsonObject = JSONObject.parseObject(res);
            if(1016 == jsonObject.getIntValue("status")){
                redisUtil.del(OpenApiConstant.API_TOKEN_KEY);
                throw new JeecgBootException("token失效,请稍后重试");
            }

            return 1001 == jsonObject.getIntValue("status")  ?  res : "";
        } catch (Exception e) {
            log.info("OpenApiService opGpVclPosColor Exception |{}" ,e.getMessage());
            throw new JeecgBootException(e.getMessage());
        }

    }

    /**
     * 新版删除 2021/8/4
     * 车辆最新位置查询(车架号)
     * @param vin
     * @return
     */
    public String vLastLocationVinV3(String vin ) {
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
        }
        try {
            Map<String, String> map = new HashMap<>();
            String token = this.getLoginToken();

            if(StringUtils.isEmpty(token)){
                throw new JeecgBootException("获取token失败,请稍后重试。");
            }
            map.put("token", token);
            map.put("srt", systemConfig.getZjxlApiPrivateKey());
            map.put("cid", systemConfig.getZjxlApiClientid());
            map.put("vin", vin);
            map.put("timeNearby", "30");
            String url = API_DOMAIN+"vLastLocationVinV3";

            DataExchangeService des = getDataExchangeService();
            String res = des.postHttps(url, map);
            if(StringUtils.isBlank(res)){
                throw new JeecgBootException("数据获取异常,请稍后重试");
            }
            JSONObject jsonObject = JSONObject.parseObject(res);
            if(1016 == jsonObject.getIntValue("status")){
                redisUtil.del(OpenApiConstant.API_TOKEN_KEY);
                throw new JeecgBootException("token失效,请稍后重试");
            }

            return 1001 == jsonObject.getIntValue("status")  ?  res : "";
        } catch (Exception e) {
            log.info("OpenApiService vLastLocationVinV3 Exception |{}" ,e.getMessage());
            throw new JeecgBootException(e.getMessage());
        }

    }

    /**
     * 新版删除 2021/8/4
     * 多车最新位置查询
     * @param vclNs 车牌号_车牌颜色, 列表(以半角逗 号连接) 车牌颜色，1:蓝色;2:黄色 示例:京 xxx_1,京 yyy_2,京 zzz_2,京 mmm_2
     * @return
     */
    public String vLastLocationMultiV4(String vclNs ) {
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
        }
        try {
            Map<String, String> map = new HashMap<>();
            String token = this.getLoginToken();

            if(StringUtils.isEmpty(token)){
                throw new JeecgBootException("获取token失败,请稍后重试。");
            }
            map.put("token", token);
            map.put("srt", systemConfig.getZjxlApiPrivateKey());
            map.put("cid", systemConfig.getZjxlApiClientid());
            map.put("vclNs", vclNs);
            map.put("timeNearby", "30");
            String url = API_DOMAIN+"vLastLocationMultiV4";

            DataExchangeService des = getDataExchangeService();
            String res = des.postHttps(url, map);
            if(StringUtils.isBlank(res)){
                throw new JeecgBootException("数据获取异常,请稍后重试");
            }
            JSONObject jsonObject = JSONObject.parseObject(res);
            if(1016 == jsonObject.getIntValue("status")){
                redisUtil.del(OpenApiConstant.API_TOKEN_KEY);
                throw new JeecgBootException("token失效,请稍后重试");
            }

            return 1001 == jsonObject.getIntValue("status")  ?  res : "";
        } catch (Exception e) {
            log.info("OpenApiService vLastLocationMultiV4 Exception |{}" ,e.getMessage());
            throw new JeecgBootException(e.getMessage());
        }

    }


    /**
     * 订单路径跟踪 5.1.3.3.1. 运输行程服务
     *
     * 近6个月自然月，  与开始时间相差72小时之内
     * @param vclN 车牌号
     * @param vco  车俩颜色
     * @param qryBtm
     * @param qryEtm
     * @param parkMins 停车时间 否，不填默认 10min
     *
     * @return
     */
    public String routerPath(String vclN,String vco ,String qryBtm ,String qryEtm ,String parkMins ){
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
        }
        try {
            Map<String, String> map = new HashMap<>();
            String token = this.getLoginToken();

            if(StringUtils.isEmpty(token)){
                throw new JeecgBootException("中交兴路获取token失败,请稍后重试。");
            }
            map.put("token", token);
            map.put("srt", systemConfig.getZjxlApiPrivateKey());
            map.put("cid", systemConfig.getZjxlApiClientid());
            map.put("vclN", vclN);
            map.put("vco", vco);
            map.put("qryBtm", qryBtm);
            map.put("qryEtm", qryEtm);
            map.put("parkMins", StringUtils.isBlank(parkMins) || parkMins.equals("0")  ? "10" : parkMins);

            String url = API_DOMAIN + "routerPath";

            log.info("获取中交轨迹请求参数, {}, {}", url, JSONUtil.toJsonStr(map));
            DataExchangeService des = getDataExchangeService();
            String res = des.postHttps(url, map);
            if(StringUtils.isBlank(res)){
                log.info("OpenApiService routerPath  result|{}" ,res);
                throw new JeecgBootException("中交兴路数据获取异常,请稍后重试");
            }

            JSONObject jsonObject = JSONObject.parseObject(res);
            int status = jsonObject.getIntValue("status") ;
            if(ErrorCodeEnum.isValid(status)){
                if(1001 == status){
                    return    res ;
                }
                if(1016 == status || 1031 == status){
                    redisUtil.del(OpenApiConstant.API_TOKEN_KEY);
                    throw new JeecgBootException("中交兴路token失效,请稍后重试");
                }
                ErrorCodeEnum errorCodeEnum =  ErrorCodeEnum.getErrorCodeEnumByVal(status);
                throw new JeecgBootException("车辆轨迹:"+errorCodeEnum.getMsg());
            } else {
                throw new JeecgBootException("车辆轨迹数据获取异常:请稍后重试");
            }
        } catch (Exception e) {
            log.info("OpenApiService routerPath Exception |{}" ,e.getMessage());
            e.printStackTrace();
            throw new JeecgBootException(e.getMessage());
        }

    }

    /**
     * 新版删除 2021/8/4
     * 车辆轨迹查询(车牌号)
     * 开始时间和结束时间不 能超过 24 小时。
     * @param vclN
     * @param qryBtm 2020-12-14 00:00:00
     * @param qryEtm 2020-12-14 23:59:59
     */
    public String vHisTrack24(String vclN,String qryBtm ,String qryEtm){
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
        }
        try {
            Map<String, String> map = new HashMap<>();
            String token = this.getLoginToken();

            if(StringUtils.isEmpty(token)){
                throw new JeecgBootException("获取token失败,请稍后重试。");
            }
            map.put("token", token);
            map.put("srt", systemConfig.getZjxlApiPrivateKey());
            map.put("cid", systemConfig.getZjxlApiClientid());
            map.put("vclN", vclN);
            map.put("qryBtm", qryBtm);
            map.put("qryEtm", qryEtm);
            String url = API_DOMAIN+"vHisTrack24";

            DataExchangeService des = getDataExchangeService();
            String res = des.postHttps(url, map);
            if(StringUtils.isBlank(res)){
                throw new JeecgBootException("数据获取异常,请稍后重试");
            }
            JSONObject jsonObject = JSONObject.parseObject(res);
            if(1016 == jsonObject.getIntValue("status")){
                redisUtil.del(OpenApiConstant.API_TOKEN_KEY);
                throw new JeecgBootException("token失效,请稍后重试");
            }

            return 1001 == jsonObject.getIntValue("status")  ?  res : "";
        } catch (Exception e) {
            log.info("OpenApiService vHisTrack24 Exception |{}" ,e.getMessage());
            throw new JeecgBootException(e.getMessage());
        }

    }

    /**
     * 新版删除 2021/8/4
     * 车辆轨迹查询(车牌号+颜色)
     * @param vclN 车牌号_车牌颜色(1.蓝牌车， 2.黄牌车)
     * @param qryBtm
     * @param qryEtm
     * @return
     */
    public String vHisTrack24V4(String vclN,String qryBtm ,String qryEtm){
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
        }
        try {
            Map<String, String> map = new HashMap<>();
            String token = this.getLoginToken();

            if(StringUtils.isEmpty(token)){
                throw new JeecgBootException("获取token失败,请稍后重试。");
            }
            map.put("token", token);
            map.put("srt", systemConfig.getZjxlApiPrivateKey());
            map.put("cid", systemConfig.getZjxlApiClientid());
            map.put("vclN", vclN);
            map.put("qryBtm", qryBtm);
            map.put("qryEtm", qryEtm);
            String url = API_DOMAIN+"vHisTrack24V4";

            DataExchangeService des = getDataExchangeService();
            String res = des.postHttps(url, map);
            if(StringUtils.isBlank(res)){
                throw new JeecgBootException("数据获取异常,请稍后重试");
            }
            JSONObject jsonObject = JSONObject.parseObject(res);
            if(1016 == jsonObject.getIntValue("status")){
                redisUtil.del(OpenApiConstant.API_TOKEN_KEY);
                throw new JeecgBootException("token失效,请稍后重试");
            }

            return 1001 == jsonObject.getIntValue("status")  ?  res : "";
        } catch (Exception e) {
            log.info("OpenApiService vHisTrack24V4 Exception |{}" ,e.getMessage());
            throw new JeecgBootException(e.getMessage());
        }

    }

    /**
     * 新版删除 2021/8/4
     * 车辆轨迹查询(车架号)
     * @param vin
     * @param qryBtm
     * @param qryEtm
     * @return
     */
    public String vHisTrackVin24(String vin,String qryBtm ,String qryEtm){
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
        }
        try {
            Map<String, String> map = new HashMap<>();
            String token = this.getLoginToken();

            if(StringUtils.isEmpty(token)){
                throw new JeecgBootException("获取token失败,请稍后重试。");
            }
            map.put("token", token);
            map.put("srt", systemConfig.getZjxlApiPrivateKey());
            map.put("cid", systemConfig.getZjxlApiClientid());
            map.put("vin", vin);
            map.put("qryBtm", qryBtm);
            map.put("qryEtm", qryEtm);
            String url = API_DOMAIN+"vHisTrackVin24";

            DataExchangeService des = getDataExchangeService();
            String res = des.postHttps(url, map);
            if(StringUtils.isBlank(res)){
                throw new JeecgBootException("数据获取异常,请稍后重试");
            }
            JSONObject jsonObject = JSONObject.parseObject(res);
            if(1016 == jsonObject.getIntValue("status")){
                redisUtil.del(OpenApiConstant.API_TOKEN_KEY);
                throw new JeecgBootException("token失效,请稍后重试");
            }

            return 1001 == jsonObject.getIntValue("status")  ?  res : "";
        } catch (Exception e) {
            log.info("OpenApiService vHisTrackVin24 Exception |{}" ,e.getMessage());
            throw new JeecgBootException(e.getMessage());
        }

    }

    /**
     * 新版删除 2021/10/14
     * 车辆入网验证(车牌号)
     * @param vclN
     * @return
     */
    public String checkTruckExist(String vclN){
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
        }
        try {
            Map<String, String> map = new HashMap<>();
            String token = this.getLoginToken();

            if(StringUtils.isEmpty(token)){
                throw new JeecgBootException("获取token失败,请稍后重试。");
            }
            map.put("token", token);
            map.put("srt", systemConfig.getZjxlApiPrivateKey());
            map.put("cid", systemConfig.getZjxlApiClientid());
            map.put("vclN", vclN);
            String url = API_DOMAIN+"checkTruckExist";

            DataExchangeService des = getDataExchangeService();
            String res = des.postHttps(url, map);
            if(StringUtils.isBlank(res)){
                throw new JeecgBootException("数据获取异常,请稍后重试");
            }
            JSONObject jsonObject = JSONObject.parseObject(res);
            if(1016 == jsonObject.getIntValue("status")){
                redisUtil.del(OpenApiConstant.API_TOKEN_KEY);
                throw new JeecgBootException("token失效,请稍后重试");
            }

            return 1001 == jsonObject.getIntValue("status") ?  res : "";
        } catch (Exception e) {
            log.info("OpenApiService checkTruckExist Exception |{}" ,e.getMessage());
            throw new JeecgBootException(e.getMessage());
        }

    }


    /**
     * 新版 2023/05/15
     * 车辆入网验证(车牌号_颜色，例如：京 xxx_2)
     * @param vclN
     * @return
     */
    public String checkVehicleExistV2(String vclN){
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
        }

        try {
            Map<String, String> map = new HashMap<>();
            String token = this.getLoginToken();
            if (StringUtils.isEmpty(token)){
                log.error("checkTruckExistV2 获取token失败,请稍后重试。");
                return StrUtil.EMPTY;
            }
            map.put("token", token);
            map.put("srt", systemConfig.getZjxlApiPrivateKey());
            map.put("cid", systemConfig.getZjxlApiClientid());
            map.put("vclN", vclN);
            String url = API_DOMAIN + "checkVehicleExistV2";

            log.info("发送车辆入网验证请求-> {} , {}", url, JSONUtil.toJsonStr(map));
            DataExchangeService des = getDataExchangeService();
            String res = des.postHttps(url, map);
            log.info("发送车辆入网验证回执<- {}", res);
            if (StringUtils.isBlank(res)){
                log.error("checkTruckExistV2 数据获取异常,请稍后重试");
                return StrUtil.EMPTY;
            }

            JSONObject jsonObject = JSONObject.parseObject(res);
            if (1016 == jsonObject.getIntValue("status")){
                redisUtil.del(OpenApiConstant.API_TOKEN_KEY);
                log.error("checkTruckExistV2 token失效,请稍后重试");
                return StrUtil.EMPTY;
            }

            return res;
        } catch (Exception e) {
            e.printStackTrace();
            log.info("中交车辆入网验证异常 ,{}" ,e.getMessage());
        }

        return StrUtil.EMPTY;
    }


    /**
     * 新版删除 2021/8/4
     * 车主真实性验证接口
     * @param vclN 车牌号码
     * @param ownerName 车主姓名
     * @param ownerPhone 车主电话 非必要
     * @return
     */
    public String checkOwnerByVclNo(String vclN,String ownerName,String ownerPhone){
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
        }
        try {
            Map<String, String> map = new HashMap<>();
            String token = this.getLoginToken();

            if(StringUtils.isEmpty(token)){
                throw new JeecgBootException("获取token失败,请稍后重试。");
            }
            map.put("token", token);
            map.put("srt", systemConfig.getZjxlApiPrivateKey());
            map.put("cid", systemConfig.getZjxlApiClientid());
            map.put("vclN", vclN);
            map.put("ownerName", ownerName);
            map.put("ownerPhone", ownerPhone);
            String url = API_DOMAIN+"checkOwnerByVclNo";

            DataExchangeService des = getDataExchangeService();
            String res = des.postHttps(url, map);
            if(StringUtils.isBlank(res)){
                throw new JeecgBootException("数据获取异常,请稍后重试");
            }
            JSONObject jsonObject = JSONObject.parseObject(res);
            if(1016 == jsonObject.getIntValue("status")){
                redisUtil.del(OpenApiConstant.API_TOKEN_KEY);
                throw new JeecgBootException("token失效,请稍后重试");
            }

            return 1001 == jsonObject.getIntValue("status")  ?  res : "";
        } catch (Exception e) {
            log.info("OpenApiService checkOwnerByVclNo Exception |{}" ,e.getMessage());
            throw new JeecgBootException(e.getMessage());
        }

    }

    /**
     * 车辆道路运输证验证接口 V2
     * 结果可查询到 道路运输证号
     * 道路运输证号和车架号(二选一)
     * @param vno 车牌号
     * @param vco 车牌颜色(1 蓝牌、2 黄牌、3 绿牌)
     * @param vehicleRoadLicense 道路运输证号
     * @param vin 车架号
     * @return
     */
    public String checkRTCNoV2(String vno,String vco,String vehicleRoadLicense, String vin){
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
        }
        try {
            Map<String, String> map = new HashMap<>();
            String token = this.getLoginToken();

            if(StringUtils.isEmpty(token)){
                throw new JeecgBootException("获取token失败,请稍后重试。");
            }
            map.put("token", token);
            map.put("srt", systemConfig.getZjxlApiPrivateKey());
            map.put("cid", systemConfig.getZjxlApiClientid());

            map.put("vclN", vno);
            map.put("vco", vco);
            map.put("vno", vno);

            if(StringUtils.isNotEmpty(vehicleRoadLicense) && StringUtils.isNotEmpty(vin)){
                map.put("vehicleRoadLicense", vehicleRoadLicense);
                map.put("vin", vin);
            }else if(StringUtils.isEmpty(vehicleRoadLicense) && StringUtils.isNotEmpty(vin)){
                map.put("vin", vin);
            }else if(StringUtils.isEmpty(vin)  && StringUtils.isNotEmpty(vehicleRoadLicense) ){
                map.put("vehicleRoadLicense", vehicleRoadLicense);
            }
            //  vehicleRoadLicenseV3
            String url = API_DOMAIN+"checkRTCNoV2";

            DataExchangeService des = getDataExchangeService();
            String res = des.postHttps(url, map);
            if(StringUtils.isBlank(res)){
                throw new JeecgBootException("道路运输证:数据获取异常,请稍后重试");
            }
//            LOGGER.info("OpenApiService checkRTCNoV2 url | {} Request|{} res |{} " ,url, map ,res);
            JSONObject jsonObject = JSONObject.parseObject(res);
            int status = jsonObject.getIntValue("status") ;
            if(ErrorCodeEnum.isValid(status)){
                if(1001 == status){
                    return    res ;
                }
                if(1016 == status || 1031 == status){
                    redisUtil.del(OpenApiConstant.API_TOKEN_KEY);
                    throw new JeecgBootException("中交token失效,请稍后重试");
                }
                ErrorCodeEnum errorCodeEnum =  ErrorCodeEnum.getErrorCodeEnumByVal(status);
                throw new JeecgBootException("道路运输证数据获取异常:"+errorCodeEnum.getMsg());
            } else {
                throw new JeecgBootException("道路运输证数据获取异常:请稍后重试");
            }
        } catch (Exception e) {
            log.info("OpenApiService checkRTCNoV2 Exception |{}" ,e.getMessage());
            throw new JeecgBootException(e.getMessage());
        }

    }

    /**
     * 道路运输证详细信息查询
     * 可查询到车牌颜色
     * @param vno
     * @param vco
     * @param vehicleRoadLicense 道路运输证号
     * @return
     */
    public String vehicleRoadLicense(String vno,String vco,String vehicleRoadLicense){
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
        }
        try {
            Map<String, String> map = new HashMap<>();
            String token = this.getLoginToken();

            if(StringUtils.isEmpty(token)){
                throw new JeecgBootException("获取token失败,请稍后重试。");
            }
            map.put("token", token);
            map.put("srt", systemConfig.getZjxlApiPrivateKey());
            map.put("cid", systemConfig.getZjxlApiClientid());
            map.put("vno", vno);
            if(StringUtils.isEmpty(vco)  && StringUtils.isNotEmpty(vehicleRoadLicense) ){
                map.put("vehicleRoadLicense", vehicleRoadLicense);
            }
            if(StringUtils.isEmpty(vehicleRoadLicense) && StringUtils.isNotEmpty(vco)){
                map.put("vco", vco);
            }
            String url = API_DOMAIN+"vehicleRoadLicense";
            DataExchangeService des = getDataExchangeService();
            String res = des.postHttps(url, map);
            if(StringUtils.isBlank(res)){
                throw new JeecgBootException("数据获取异常,请稍后重试");
            }
            JSONObject jsonObject = JSONObject.parseObject(res);
            if(1016 == jsonObject.getIntValue("status")){
                redisUtil.del(OpenApiConstant.API_TOKEN_KEY);
                throw new JeecgBootException("token失效,请稍后重试");
            }

            return 1001 == jsonObject.getIntValue("status")  ?  res : "";
        } catch (Exception e) {
            log.info("OpenApiService vehicleRoadLicense Exception |{}" ,e.getMessage());
            throw new JeecgBootException(e.getMessage());
        }

    }

    /**
     * 人员从业资格证验证 V2
     * 结果可查从业资格证号
     * 身份证号和从业资格证号 (二选一)
     * @param personName 姓名
     * @param provinceCode 省份/直辖市/自治区编码
     * @param citizenNo 身份证号
     * @param userQualification 从业资格证号
     *
     *  certificateState 有效/无效
     * @return
     */
    public String checkQualificationV2(String personName,String provinceCode,
                                       String citizenNo, String userQualification){
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
        }
        try {
            Map<String, String> map = new HashMap<>();
            String token = this.getLoginToken();
            if(StringUtils.isEmpty(token)){
                throw new JeecgBootException("获取token失败,请稍后重试。");
            }
            map.put("token", token);
            map.put("srt", systemConfig.getZjxlApiPrivateKey());
            map.put("cid", systemConfig.getZjxlApiClientid());
            map.put("idCardName", personName);
            map.put("rCdNm", provinceCode);
            if(StringUtils.isEmpty(userQualification)  && StringUtils.isNotEmpty(citizenNo) ){
                map.put("citizenNo", citizenNo);
            }

            map.put("personName", personName);
            map.put("provinceCode", provinceCode);

            if(StringUtils.isEmpty(citizenNo) && StringUtils.isNotEmpty(userQualification)){
                map.put("userQualification", userQualification);
            }
            //checkQualificationV2 queryQualificationInfoV3
            String url = API_DOMAIN+"checkQualificationV2";
            DataExchangeService des = getDataExchangeService();
            String res = des.postHttps(url, map);
            if(StringUtils.isBlank(res)){
                throw new JeecgBootException("从业资格证号数据获取异常,请稍后重试");
            }
//            LOGGER.info("OpenApiService checkQualificationV2 url|{} result|{} res |{} " ,url, map ,res);
            JSONObject jsonObject = JSONObject.parseObject(res);
            int status = jsonObject.getIntValue("status") ;
            if(ErrorCodeEnum.isValid(status)){
                if(1001 == status){
                    return    res ;
                }
                if(1016 == status || 1031 == status){
                    redisUtil.del(OpenApiConstant.API_TOKEN_KEY);
                    throw new JeecgBootException("中交token失效,请稍后重试");
                }
                ErrorCodeEnum errorCodeEnum =  ErrorCodeEnum.getErrorCodeEnumByVal(status);
                throw new JeecgBootException("从业资格证号数据获取异常:"+errorCodeEnum.getMsg());
            } else {
                throw new JeecgBootException("从业资格证号数据获取异常:请稍后重试");
            }
        } catch (Exception e) {
            log.info("OpenApiService checkQualificationV2 Exception |{}" ,e.getMessage());
            throw new JeecgBootException(e.getMessage());
        }

    }

    /**
     * 车辆行驶证信息查询 V2
     * @param vclN 车牌号
     * @param vco 车牌颜色 1:蓝色; 2:黄色
     * @return
     */
    public String vQueryLicenseV2(String vclN,String vco){
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
        }
        try {
            Map<String, String> map = new HashMap<>();
            String token = this.getLoginToken();
            if(StringUtils.isEmpty(token)){
                throw new JeecgBootException("获取token失败,请稍后重试。");
            }
            map.put("token", token);
            map.put("srt", systemConfig.getZjxlApiPrivateKey());
            map.put("cid", systemConfig.getZjxlApiClientid());
            map.put("vclN", vclN);
            map.put("vco", vco);
            String url = API_DOMAIN+"vQueryLicenseV2";

            DataExchangeService des = getDataExchangeService();
            String res = des.postHttps(url, map);
            if(StringUtils.isBlank(res)){
                throw new JeecgBootException("数据获取异常,请稍后重试");
            }
            JSONObject jsonObject = JSONObject.parseObject(res);
            if(1016 == jsonObject.getIntValue("status")){
                redisUtil.del(OpenApiConstant.API_TOKEN_KEY);
                throw new JeecgBootException("token失效,请稍后重试");
            }

            return 1001 == jsonObject.getIntValue("status") ?  res : "";
        } catch (Exception e) {
            log.info("OpenApiService vQueryLicenseV2 Exception |{}" ,e.getMessage());
            throw new JeecgBootException(e.getMessage());
        }

    }

    /**
     * 行驶证识别信息查询接口
     * 本接口提供行驶证识别信息查询。
     * @param vehicleLicenseImg
     */
    public String vehicleLicense(String vehicleLicenseImg) {
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
        }
        try {
            Map<String, String> map = new HashMap<String, String>(4);
            String token = this.getLoginToken();
            if(StringUtils.isEmpty(token)){
                throw new JeecgBootException("获取token失败,请联系管理。");
            }
            map.put("token", token);
            map.put("srt", systemConfig.getZjxlApiPrivateKey());
            map.put("cid", systemConfig.getZjxlApiClientid());
            // 文件路径，包含文件的扩展名
            map.put("path", vehicleLicenseImg);
            String url = API_DOMAIN+"vehicleLicense";
            // 通过 https 方式调用，此方法内部会使用私钥生成签名参数 sign,私钥不会发送
            DataExchangeService des = getDataExchangeService();
            String res = des.postHttps(url, map);
            if(StringUtils.isBlank(res)){
                throw new JeecgBootException("数据获取异常,请稍后重试");
            }
            JSONObject jsonObject = JSONObject.parseObject(res);
            if(1016 == jsonObject.getIntValue("status")){
                redisUtil.del(OpenApiConstant.API_TOKEN_KEY);
                throw new JeecgBootException("token失效,请稍后重试");
            }
            return 1001 == jsonObject.getIntValue("status") ?  res : "";

        } catch (Exception e) {
            log.info("OpenApiService vehicleLicense Exception |{}" ,e.getMessage());
            throw new JeecgBootException(e.getMessage());
        }

    }

    /**
     * 身份证识别信息查询(正、反面)
     * 本接口提供身份证识别信息查询。
     */
    public void idCardLicense() {
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
        }
        try {
            Map<String, String> map = new HashMap<String, String>(4);
            String token = this.getLoginToken();

            if(StringUtils.isEmpty(token)){
                log.info("OpenApiService idCardLicense token | {} " ,token);
                throw new JeecgBootException("获取token失败,请稍后重试。");
            }
            map.put("token", token);
            map.put("srt", systemConfig.getZjxlApiPrivateKey());
            map.put("cid", systemConfig.getZjxlApiClientid());
            // 文件路径，包含文件的扩展名
            map.put("path", "E:\\图片\\身份证\\正面.jpg");
            map.put("path2", "E:\\图片\\身份证\\背面.jpg");
            String url = "https://testopen.95155.com/save/apis/idCardLicense";
//            System.out.println("请求地址:"+url);
            // 通过 https 方式调用， 此方法内部会使用私钥生成签名参数 sign,私钥不会发送
            DataExchangeService des = new DataExchangeService(5000,5000);
            String res = des.postHttps(url, map);
//            System.out.println("返回:"+ res);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("e:" + e.getMessage());
        }
    }



    /**
     * 上报司机
     * @param zjDriverDTO
     * @return
     */
    public String reportDriver(ZjDriverDTO zjDriverDTO){
        HyConfig systemConfig =  HyConfigUtils.getConfig();

        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
        }

        Map<String, String> map = new HashMap<>();
        String token = this.getLoginToken();

        if (StringUtils.isEmpty(token)) {
            throw new JeecgBootException("中交兴路获取token失败,请稍后重试。");
        }

        BizParamDTO bizParamDTO = new BizParamDTO();
        bizParamDTO.setEnterpriseCode(systemConfig.getZjxlEnterpriseCode());
        bizParamDTO.setPlatformCode(systemConfig.getZjxlPlatformCode());
        bizParamDTO.setServiceType("driver");
        bizParamDTO.setCallbackUrl(REPORT_CALLBACK_URL);
        bizParamDTO.setEnv(REPORT_ENV);
        bizParamDTO.setData(zjDriverDTO);

        map.put("token", token);
        map.put("cid", systemConfig.getZjxlApiClientid());
        map.put("srt", systemConfig.getZjxlApiPrivateKey());
        map.put("bizParam", JSONObject.toJSONString(bizParamDTO));

        String res = StrUtil.EMPTY;
        try {
            //String url = API_REPORT_DOMAIN + "wlhyReportDriver";
            String url = API_DOMAIN + "wlhyReportDriver";
            DataExchangeService des = getDataExchangeService();

            log.info("token : {} , cid : {}, privatekey : {}", token, systemConfig.getZjxlApiClientid(), systemConfig.getZjxlApiPrivateKey());
            log.info("中交司机上报请求 -> {} {}", url, JSONUtil.toJsonStr(map));
            res = des.postHttps(url, map);
            log.info("中交司机上报回执 <- {}", JSONUtil.toJsonStr(res));
            if (StringUtils.isBlank(res)) {
                throw new JeecgBootException("中交兴路上报司机数据异常,请稍后重试");
            }

            JSONObject jsonObject = JSONObject.parseObject(res);
            int status = jsonObject.getIntValue("status") ;
            if (ErrorCodeEnum.isValid(status)) {
                if(1001 == status){
                    return  res ;
                }
                if(1016 == status || 1031 == status){
                    redisUtil.del(OpenApiConstant.API_TOKEN_KEY);
                    log.error("中交兴路token失效,请稍后重试, {}", res);
                }

                return res;
//                resultString = jsonObject.getString("result") != null ? jsonObject.getString("result") : StringUtils.EMPTY ;
//                ErrorCodeEnum errorCodeEnum =  ErrorCodeEnum.getErrorCodeEnumByVal(status);
//                throw new JeecgBootException("中交兴路上报司机数据异常:"+errorCodeEnum.getMsg()+resultString);
            }
        } catch (Exception e) {
            log.info("中交兴路上报司机异常 {}", e.getMessage());
        }

        return res;
    }

    /**
     *
     * @param zjVehicleDTO
     * @return
     */
    public String reportVehicle(ZjVehicleDTO zjVehicleDTO){
        HyConfig systemConfig =  HyConfigUtils.getConfig();

        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
        }

        Map<String, String> map = new HashMap<>();
        String token = this.getLoginToken();

        if (StringUtils.isEmpty(token)) {
            throw new JeecgBootException("中交兴路获取token失败,请稍后重试。");
        }

        BizParamDTO bizParamDTO = new BizParamDTO();
        bizParamDTO.setEnterpriseCode(systemConfig.getZjxlEnterpriseCode());
        bizParamDTO.setPlatformCode(systemConfig.getZjxlPlatformCode());
        bizParamDTO.setServiceType("vehicle");
        bizParamDTO.setCallbackUrl(REPORT_CALLBACK_URL);
        bizParamDTO.setEnv(REPORT_ENV);
        bizParamDTO.setData(zjVehicleDTO);

        map.put("token", token);
        map.put("cid", systemConfig.getZjxlApiClientid());
        map.put("srt", systemConfig.getZjxlApiPrivateKey());
        map.put("bizParam", JSONObject.toJSONString(bizParamDTO));

        String res = StrUtil.EMPTY;

        try {
            String url = API_DOMAIN + "wlhyReportVehicle";
            DataExchangeService des = getDataExchangeService();
            log.info("token : {} , cid : {}, privatekey : {}", token, systemConfig.getZjxlApiClientid(), systemConfig.getZjxlApiPrivateKey());
            log.info("中交车辆上报请求 -> {} {}", url, JSONUtil.toJsonStr(map));
            res = des.postHttps(url, map);
            log.info("中交车辆上报回执 <- {}", JSONUtil.toJsonStr(res));

            if (StringUtils.isBlank(res)) {
                throw new JeecgBootException("中交兴路上报车辆数据异常,请稍后重试");
            }

            JSONObject jsonObject = JSONObject.parseObject(res);
            int status = jsonObject.getIntValue("status") ;
            if(ErrorCodeEnum.isValid(status)){
                if(1001 == status){
                    return    res ;
                }
                if(1016 == status || 1031 == status){
                    redisUtil.del(OpenApiConstant.API_TOKEN_KEY);
                    log.error("中交兴路token失效,请稍后重试, {}", res);
                }

                return res;
            }
        } catch (Exception e) {
            log.info("中交兴路上报车辆异常 {}", e.getMessage());
        }

        return res;
    }

    /**
     *
     * @param waybillDTO
     * @return
     */
    public String  reportWaybill(WaybillDTO waybillDTO){
        HyConfig systemConfig =  HyConfigUtils.getConfig();

        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
        }

        Map<String, String> map = new HashMap<>();
        String token = this.getLoginToken();

        if (StringUtils.isEmpty(token)) {
            throw new JeecgBootException("中交兴路获取token失败,请稍后重试。");
        }

        BizParamDTO bizParamDTO = new BizParamDTO();
        bizParamDTO.setEnterpriseCode(systemConfig.getZjxlEnterpriseCode());
        bizParamDTO.setPlatformCode(systemConfig.getZjxlPlatformCode());
        bizParamDTO.setServiceType("waybill");
        bizParamDTO.setCallbackUrl(REPORT_CALLBACK_URL);
        bizParamDTO.setEnv(REPORT_ENV);
        bizParamDTO.setData(waybillDTO);

        map.put("token", token);
        map.put("cid", systemConfig.getZjxlApiClientid());
        map.put("srt", systemConfig.getZjxlApiPrivateKey());
        map.put("bizParam", JSONObject.toJSONString(bizParamDTO));

        String res = StrUtil.EMPTY;
        try {
            String url = API_DOMAIN + "wlhyReportWaybill";
            DataExchangeService des = getDataExchangeService();

            log.info("token : {} , cid : {}, privatekey : {}", token, systemConfig.getZjxlApiClientid(), systemConfig.getZjxlApiPrivateKey());
            log.info("中交运单上报请求 -> {} {}", url, JSONUtil.toJsonStr(map));
            res = des.postHttps(url, map);
            log.info("中交运单上报回执 <- {}", JSONUtil.toJsonStr(res));

            if(StringUtils.isBlank(res)){
                throw new JeecgBootException("中交兴路上报运单数据获取异常,请稍后重试");
            }

            JSONObject jsonObject = JSONObject.parseObject(res);
            int status = jsonObject.getIntValue("status") ;
            if(ErrorCodeEnum.isValid(status)){
                if(1001 == status){
                    return    res ;
                }
                if(1016 == status || 1031 == status){
                    redisUtil.del(OpenApiConstant.API_TOKEN_KEY);
                    throw new JeecgBootException("中交兴路token失效,请稍后重试");
                }

                return res;
//                String resultString = jsonObject.getString("result") != null ? jsonObject.getString("result") : StringUtils.EMPTY ;
//                ErrorCodeEnum errorCodeEnum =  ErrorCodeEnum.getErrorCodeEnumByVal(status);
//                throw new JeecgBootException("中交兴路上报运单数据获取异常:"+errorCodeEnum.getMsg()+resultString);
            }
        } catch (Exception e) {
            log.info("OpenApiService reportWaybill Exception |{}" ,e.getMessage());
        }

        return res;
    }

    /**
     *
     * @param capitalDTO
     * @return
     */
    public String reportCapital(CapitalDTO capitalDTO) {

        HyConfig systemConfig =  HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
        }

        Map<String, String> map = new HashMap<>();
        String token = this.getLoginToken();

        if (StringUtils.isEmpty(token)) {
            throw new JeecgBootException("中交兴路获取token失败,请稍后重试。");
        }

        BizParamDTO bizParamDTO = new BizParamDTO();
        bizParamDTO.setEnterpriseCode(systemConfig.getZjxlEnterpriseCode());
        bizParamDTO.setPlatformCode(systemConfig.getZjxlPlatformCode());
        bizParamDTO.setServiceType("capital");
        bizParamDTO.setCallbackUrl(REPORT_CALLBACK_URL);
        bizParamDTO.setEnv(REPORT_ENV);
        bizParamDTO.setData(capitalDTO);

        map.put("token", token);
        map.put("cid", systemConfig.getZjxlApiClientid());
        map.put("srt", systemConfig.getZjxlApiPrivateKey());
        map.put("bizParam", JSONObject.toJSONString(bizParamDTO));

        String res = StrUtil.EMPTY;
        try {
            String url = API_DOMAIN + "wlhyReportCapital";
            DataExchangeService des = getDataExchangeService();
            log.info("token : {} , cid : {}, privatekey : {}", token, systemConfig.getZjxlApiClientid(), systemConfig.getZjxlApiPrivateKey());
            log.info("中交资金上报请求 -> {} {}", url, JSONUtil.toJsonStr(map));
            res = des.postHttps(url, map);
            log.info("中交资金上报回执 <- {}", JSONUtil.toJsonStr(res));

            if(StringUtils.isBlank(res)){
                throw new JeecgBootException("中交兴路上报资金流水数据异常,请稍后重试");
            }

            JSONObject jsonObject = JSONObject.parseObject(res);
            int status = jsonObject.getIntValue("status") ;
            if(ErrorCodeEnum.isValid(status)){
                if(1001 == status) {
                    return    res ;
                }
                if(1016 == status || 1031 == status){
                    redisUtil.del(OpenApiConstant.API_TOKEN_KEY);
                    throw new JeecgBootException("中交兴路token失效,请稍后重试");
                }

                return res;
//                String resultString = jsonObject.getString("result") != null ? jsonObject.getString("result") : StringUtils.EMPTY ;
//                ErrorCodeEnum errorCodeEnum =   ErrorCodeEnum.getErrorCodeEnumByVal(status);
//                throw new JeecgBootException("中交兴路上报资金流水数据异常:"+errorCodeEnum.getMsg()+resultString);
            } else {
                throw new JeecgBootException("中交兴路上报资金流水数据异常:请稍后重试");
            }
        } catch (Exception e) {
            log.info("OpenApiService reportCapital Exception |{}" ,e.getMessage());
        }

        return res;
    }



    public static void main(String[] args)throws Exception {

        String vehicleRoadLicense ="null";
        String vin ="null";

        Map<String, String> map = new HashMap<>();

        if(StringUtils.isNotEmpty(vehicleRoadLicense) && StringUtils.isNotEmpty(vin)){
            map.put("vehicleRoadLicense", vehicleRoadLicense);
            map.put("vin", vin);
        }else if(StringUtils.isEmpty(vehicleRoadLicense) && StringUtils.isNotEmpty(vin)){
            map.put("vin", vin);
        }else if(StringUtils.isEmpty(vin)  && StringUtils.isNotEmpty(vehicleRoadLicense) ){
            map.put("vehicleRoadLicense", vehicleRoadLicense);
        }
        System.out.println(map);

//
//        HyConfig systemConfig = new HyConfig() ;
//        systemConfig.setZjxlIzUse(true);
//        systemConfig.setZjxlApiAccount("c03ea706-99dd-495e-b11c-cba84cf501a8");
//        systemConfig.setZjxlApiPassword("F2aL6d64XAd593J35644X951U0xY87");
//        systemConfig.setZjxlApiClientid("872ea103-7f66-4faf-9d31-cb10191ae4f0");
//        systemConfig.setZjxlApiPrivateKey("6d73c68a-5080-4f6c-9849-e6f4cfc72ddd");
//
//        systemConfig.setZjxlPlatformCode("shanxi");
//        systemConfig.setZjxlEnterpriseCode("8b544140-ad33-422a-9bd4-853c71fa0b90");
//
//
//        OpenApiService demo = new OpenApiService();
//
//        String token = demo.getLoginToken();
//
//        //ZjDriverDTO zjDriverDTO =   getZjDriverDTO();
//        //ZjVehicleDTO zjDriverDTO =   getZjVehicleDTO();
//        //WaybillDTO zjDriverDTO =   getWaybillDTO();
//        CapitalDTO zjDriverDTO =   getCapitalDTO();
//
//        BizParamDTO bizParamDTO = new BizParamDTO();
//        bizParamDTO.setEnterpriseCode(systemConfig.getZjxlEnterpriseCode());
//        bizParamDTO.setPlatformCode(systemConfig.getZjxlPlatformCode());
//        //bizParamDTO.setServiceType("driver");
//        //bizParamDTO.setServiceType("vehicle");
//        //bizParamDTO.setServiceType("waybill");
//        bizParamDTO.setServiceType("capital");
//        bizParamDTO.setCallbackUrl("");
//        bizParamDTO.setEnv(REPORT_ENV);
//        bizParamDTO.setData(zjDriverDTO);
//
//
//        Map<String, String> map = new HashMap();
//        map.put("token",token);
//        map.put("cid",systemConfig.getZjxlApiClientid());
//        map.put("srt", systemConfig.getZjxlApiPrivateKey());
//        map.put("bizParam", JSONObject.toJSONString(bizParamDTO) );
//
//        //String url = demo.API_DOMAIN+"wlhyReportDriver";
//        //String url = demo.API_DOMAIN+"wlhyReportVehicle";
//        //String url = demo.API_DOMAIN+"wlhyReportWaybill";
//        String url = demo.API_DOMAIN+"wlhyReportCapital";
//
//        DataExchangeService des = demo.getDataExchangeService();
//        System.out.println(url);
//        System.out.println(map);
//        String res = des.postHttps(url, map);
//        System.out.println(res);




         //demo.login();
         //demo.vLastLocationV3();
         //demo.vHisTrack24();
         //demo.vehicleLicense();
         //demo.idCardLicense();
    }
    private static CapitalDTO  getCapitalDTO(){
        CapitalDTO financialDTO = new CapitalDTO();
        financialDTO.setSerialNo(UUID.randomUUID().toString().replace("-", ""));
        financialDTO.setDocumentNumber("202206091439246724728933757");
        financialDTO.setSendToProDateTime(
                "20220609112327"
        );
        financialDTO.setCarrier("杨正林");
        financialDTO.setActualCarrierID("410521198509150530");
        financialDTO.setVehicleNumber("豫EC6701");
        financialDTO.setVehiclePlateColorCode("1");

        //运单列表
        List <ZjShippingNoteList> shippingNoteListDTOS = new ArrayList <>();
        ZjShippingNoteList shippingNoteListDTO = new ZjShippingNoteList();
        String shippingNoteNumber = "YD202206091123207367";
        if(shippingNoteNumber.length() > 20 ){
            shippingNoteNumber =  shippingNoteNumber.substring(0, 20);
        }
        shippingNoteListDTO.setShippingNoteNumber(shippingNoteNumber);
        shippingNoteListDTO.setSerialNumber("0000");
        shippingNoteListDTO.setTotalMonetaryAmount(
                "1.500"
        );

        shippingNoteListDTOS.add(shippingNoteListDTO);
        financialDTO.setShippingNoteList(shippingNoteListDTOS);
        //财务列表
        List<ZjFinanciallist>  financiallistDTOS = new ArrayList <>();
        ZjFinanciallist financiallistDTO = new ZjFinanciallist();

        financiallistDTO.setPaymentMeansCode(
                PaymentMeansCodeEnum.PAYMENT_MEANS_TYPE_YHZZ.code
        );
        financiallistDTO.setReceiptAccount("2206090006339044" );
        financiallistDTO.setRecipient("杨正林");
        //financiallistDTO.setBankCode(shippingNoteBillDb.getBankCode());
        financiallistDTO.setSequenceCode("202206091439246724728933757");
        financiallistDTO.setMonetaryAmount(
                "1.500"

        );
        financiallistDTO.setDateTime(
                "20220609112328"
        );

        financiallistDTOS.add(financiallistDTO);
        financialDTO.setFinanciallist(financiallistDTOS);
        return financialDTO;
    }


    private static WaybillDTO getWaybillDTO() {

        WaybillDTO shippingNoteDTO = new WaybillDTO();
        shippingNoteDTO.setSerialNo(UUID.randomUUID().toString().replace("-", ""));
        shippingNoteDTO.setOriginalDocumentNumber("YD202206091123207362");
        String shippingNoteNumber = "YD202206091123207362";
        if (shippingNoteNumber.length() > 20) {
            shippingNoteNumber = shippingNoteNumber.substring(0, 20);
        }
        shippingNoteDTO.setShippingNoteNumber(shippingNoteNumber);

        //Integer vehicleAmounthy = getVehicleAmounthyByOrderId(hyShippingNote.getOrderId()) ;
        shippingNoteDTO.setVehicleAmount("1");
        //分段分单号 分段运输和多车运输由 四位数字组成， 前两位代表一 单多车的序号，后两位代表分 段序号。
        //若运输形式为一单一 车填 0000。
        shippingNoteDTO.setSerialNumber("0000");
        //运输组织类型代码
        shippingNoteDTO.setTransportTypeCode("1");
        shippingNoteDTO.setSendToProDateTime(DateUtils.dateToStringDateTime(new Date()));

        shippingNoteDTO.setCarrier("网络科技");
        shippingNoteDTO.setUnifiedSocialCreditIdentifier("91440400MA56KGJC3P" );
        //车辆 道路运输经营许可 网络货运经营者的道路 运输经营许可证编号。
        shippingNoteDTO.setPermitNumber("1101011023323");

        shippingNoteDTO.setConsignmentDateTime(
                "20220609112328"
        );
        shippingNoteDTO.setBusinessTypeCode("1002996");

        shippingNoteDTO.setDespatchActualDateTime(
                "20220609112328"
        );
        shippingNoteDTO.setGoodsReceiptDateTime(
                "20220609112328"
        );

        shippingNoteDTO.setTotalMonetaryAmount("1.500");
        //托运人信息
        getZjConsignorInfo(shippingNoteDTO);
        //收货方信息
        getZjConsigneeInfo(shippingNoteDTO);
        //车辆信息
        getZjVehicleInfo(shippingNoteDTO);
        //实际承运人信息
        getZjActualCarrierInfo( shippingNoteDTO);
        //保险信息
        getZjInsuranceInformation(shippingNoteDTO);

        return shippingNoteDTO;
    }
    private static void getZjConsignorInfo( WaybillDTO shippingNoteDTO) {
        ZjConsignorInfo consignorInfo = new ZjConsignorInfo();

        consignorInfo.setConsignor("网络科技");
        consignorInfo.setConsignorID("91440400MA56KGJC3P");
        consignorInfo.setPlaceOfLoading("110101");
        consignorInfo.setCountrySubdivisionCode("");

        shippingNoteDTO.setConsignorInfo(
                Collections.singletonList(  consignorInfo)
        );
    }

    private static void getZjConsigneeInfo( WaybillDTO shippingNoteDTO) {
        ZjConsigneeInfo consigneeInfo = new ZjConsigneeInfo();

        consigneeInfo.setConsignee("李荣");
        consigneeInfo.setGoodsReceiptPlace("五十四所产业园");
        consigneeInfo.setCountrySubdivisionCode("130110");

        shippingNoteDTO.setConsigneeInfo(
                Collections.singletonList( consigneeInfo) );
    }
    private static void getZjVehicleInfo(WaybillDTO shippingNoteDTO) {

        ZjVehicleInfo vehicleInfo = new ZjVehicleInfo();
        vehicleInfo.setVehiclePlateColorCode("1");
        vehicleInfo.setVehicleNumber("豫EC6701");
        vehicleInfo.setDespatchActualDateTime(
              "20220609112328"
        );
        vehicleInfo.setGoodsReceiptDateTime(
                "20220610112328"
        );
        vehicleInfo.setPlaceOfLoading("河北省邢台市信都区中华大街道育才路29号");
        vehicleInfo.setGoodsReceiptPlace("五十四所产业园");
        vehicleInfo.setLoadingCountrySubdivisionCode("130503");
        vehicleInfo.setReceiptCountrySubdivisionCode("130110");
        //司机信息
        getZjDrivers( vehicleInfo);
        //货物信息
        getZjGoodsInfo( vehicleInfo);

        shippingNoteDTO.setVehicleInfo(
                Collections.singletonList( vehicleInfo)
        );
    }
    private static void getZjDrivers( ZjVehicleInfo vehicleInfo) {

        ZjDriver driverDTO = new ZjDriver();
        driverDTO.setDriverName("杨正林");
        driverDTO.setDrivingLicense("410521198509150530" );

        vehicleInfo.setDriver(  Collections.singletonList( driverDTO ));
    }
    private static void getZjGoodsInfo(ZjVehicleInfo vehicleInfo) {

        ZjGoodsInfo goodsInfo = new ZjGoodsInfo();
        goodsInfo.setDescriptionOfGoods("煤炭");
        goodsInfo.setCargoTypeClassificationCode("0100");
        BigDecimal goodsWeightLoading = new BigDecimal("48.000");
        goodsInfo.setGoodsItemGrossWeight(goodsWeightLoading.setScale(3, BigDecimal.ROUND_DOWN).toString());
        //goodsInfo.setCube("1.0000");
        ////        hyOrder.getCube().setScale(4,BigDecimal.ROUND_DOWN)
        ////                .toString()
        //goodsInfo.setTotalNumberOfPackages(1);

        vehicleInfo.setGoodsInfo(
                Collections.singletonList( goodsInfo)
        );
    }

    private static void getZjActualCarrierInfo( WaybillDTO shippingNoteDTO) {
        ZjActualCarrierInfo actualCarrierInfo = new ZjActualCarrierInfo();
        actualCarrierInfo.setActualCarrierName("杨正林");
        actualCarrierInfo.setActualCarrierBusinessLicense("410521198509150530" );
        actualCarrierInfo.setActualCarrierID("410521198509150530");
        shippingNoteDTO.setActualCarrierInfo(
                Collections.singletonList(actualCarrierInfo)
        );
    }

    private static void getZjInsuranceInformation(WaybillDTO shippingNoteDTO) {
        ZjInsuranceInformation insuranceInformations = new ZjInsuranceInformation();
        insuranceInformations.setInsuranceCompanyCode("none");
        insuranceInformations.setPolicyNumber("none");
        shippingNoteDTO.setInsuranceInformation(
                Collections.singletonList(  insuranceInformations)
        );
    }


    private  static ZjDriverDTO  getZjDriverDTO(){
        ZjDriverDTO zjDriverDTO =  new ZjDriverDTO();
        zjDriverDTO.setSerialNo(UUID.randomUUID().toString().replace("-", ""));
        zjDriverDTO.setDriverName("杨正林");
        zjDriverDTO.setDrivingLicense("410521198509150530");
        zjDriverDTO.setIssuingOrganizations("河南省新乡市公安局交通警察支队");
        zjDriverDTO.setQualificationCertificate("410521198509150530");
        zjDriverDTO.setTelephone("13902301223");
        zjDriverDTO.setVehicleClass("A2");
        zjDriverDTO.setValidPeriodFrom("2020-12-12");
        zjDriverDTO.setValidPeriodTo("2025-12-12");


        return zjDriverDTO;

    }

    private  static ZjVehicleDTO  getZjVehicleDTO(){
        ZjVehicleDTO zjVehicleDTO =  new ZjVehicleDTO();
        zjVehicleDTO.setSerialNo(UUID.randomUUID().toString().replace("-", ""));
        zjVehicleDTO.setVehicleNumber("豫EC6701");
        zjVehicleDTO.setOwner("安阳友来道路运输有限公司");
        zjVehicleDTO.setIssuingOrganizations("河南省新乡市公安局交通警察支队");
        zjVehicleDTO.setVehicleTonnage("40.00");
        zjVehicleDTO.setRoadTransportCertificateNumber("410506031873");
        zjVehicleDTO.setUseCharacter("货运");
        zjVehicleDTO.setVehiclePlateColorCode("1");
        zjVehicleDTO.setVin("LG6ZNDNH9LX016618");
        zjVehicleDTO.setGrossMass("48.80");
        zjVehicleDTO.setIssueDate("2019-10-12");
        zjVehicleDTO.setVehicleEnergyType("E");
        zjVehicleDTO.setVehicleType("Q11");
        zjVehicleDTO.setRegisterDate("2019-10-12");


        return zjVehicleDTO;

    }


    /**
     *
     *中交全图车辆轨迹插件
     * @return
     */
    public String  mapOverView() {
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
        }
        try {
            Map<String, String> map = new HashMap< >();
            String token = this.getLoginToken();

            if(StringUtils.isEmpty(token)){
                throw new JeecgBootException("获取token失败,请稍后重试。");
            }
//            map.put("token", token);
            map.put("srt", systemConfig.getZjxlApiPrivateKey());
            map.put("cid", systemConfig.getZjxlApiClientid());
            map.put("type", "mapOverView");

            String url = API_DOMAIN+"pluginUrl";

            DataExchangeService des = getDataExchangeService();
            String res = des.postHttps(url, map);
            if(StringUtils.isBlank(res)){
                throw new JeecgBootException("数据获取异常,请稍后重试");
            }
            JSONObject jsonObject = JSONObject.parseObject(res);
            if(1016 == jsonObject.getIntValue("status")){
                redisUtil.del(OpenApiConstant.API_TOKEN_KEY);
                throw new JeecgBootException("token失效,请稍后重试");
            }

            return 1001 == jsonObject.getIntValue("status")  ?  res : "";
        } catch (Exception e) {
            log.info("OpenApiService vLastLocationV3 Exception |{}" ,e.getMessage());
            throw new JeecgBootException(e.getMessage());
        }

    }

//    public String subscribeToVehicles(String id) {
//
//        HyVehicle vehicle = hyVehicleService.getById(id);
//        if(vehicle==null){
//            throw new JeecgBootException("车辆信息异常。");
//        }
//
//        HyConfig systemConfig = HyConfigUtils.getConfig();
//        if(systemConfig == null || !systemConfig.getZjxlIzUse()){
//            throw new JeecgBootException("中交兴路查询服务暂未开启,请联系管理。");
//        }
//        try {
//            Map<String, String> map = new HashMap< >();
//            String token = this.getLoginToken();
//
//            if(StringUtils.isEmpty(token)){
//                throw new JeecgBootException("获取token失败,请稍后重试。");
//            }
//            map.put("token", token);
//            map.put("srt", systemConfig.getZjxlApiPrivateKey());
//            map.put("cid", systemConfig.getZjxlApiClientid());
//
//            map.put("vnos", vehicle.getVehicleNumber()+"_"+vehicle.getVehiclePlateColorCode()+"_1");
//
//            String url = API_DOMAIN+"visualVehBatchSub";
//
//            DataExchangeService des = getDataExchangeService();
//            String res = des.postHttps(url, map);
//            if(StringUtils.isBlank(res)){
//                throw new JeecgBootException("数据获取异常,请稍后重试");
//            }
//            JSONObject jsonObject = JSONObject.parseObject(res);
//            if(1016 == jsonObject.getIntValue("status")){
//                redisUtil.del(OpenApiConstant.API_TOKEN_KEY);
//                throw new JeecgBootException("token失效,请稍后重试");
//            }
//
//            return 1001 == jsonObject.getIntValue("status")  ?  res : res;
//        } catch (Exception e) {
//            log.info("OpenApiService vLastLocationV3 Exception |{}" ,e.getMessage());
//            throw new JeecgBootException(e.getMessage());
//        }
//    }
}
